﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    public class BLLInvoicePrint:BaseBLL
    {
        private ITableXtSbJd xt_sb_jd_DAL = null;
        private ITableXtDelegateBank xt_delegate_bank_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_DAL = null;
        private ITable_xt_waterproperty xt_waterproperty_DAL = null;

        public BLLInvoicePrint()
        {
            xt_sb_jd_DAL = Reflect<ITableXtSbJd>.Create("DAL_table_xt_sb_jd", "Willson.JsbManager.Dal");
            xt_delegate_bank_DAL = Reflect<ITableXtDelegateBank>.Create("DAL_table_xt_delegate_bank", "Willson.JsbManager.Dal");
            xt_company_sb_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
            xt_waterproperty_DAL = Reflect<ITable_xt_waterproperty>.Create("DAL_table_xt_waterproperty", "Willson.JsbManager.Dal");
        }

        public bool ValidateAndSave(int id,int num)
        {
            Hashtable hash = new Hashtable();
            Hashtable record = new Hashtable();
   
            hash.Add("id", id);
          
            TableXtSbJdEntity entity = xt_sb_jd_DAL.Find(hash);

            if (num > entity.Jd_cjh) return false;

            record.Add("jd_scjh",num);
            record.Add("jd_jh_add",entity.Jd_cjh - num);

            xt_sb_jd_DAL.Update(id, record);     
                  
            return true;
        }

        
        public BEInvoiceInfo GetInvoiceInfo(int jd_id, int company_id)
        {
            decimal max = 0.0m;
            
            BEInvoiceInfo info = new BEInvoiceInfo();
            Hashtable hash = new Hashtable();

            hash.Add("company_id",company_id);
            TableXtDelegateBankEntity delegate_bank_entity = xt_delegate_bank_DAL.Find(hash);

            if (delegate_bank_entity == null)
            {
                info.Name_tt = string.Empty;
            }
            else
            {
                info.Name_tt = delegate_bank_entity.Name_tt;

            }
            
            Hashtable hash2 = new Hashtable();
            hash2.Add("id",jd_id);
            TableXtSbJdEntity sb_jd_entity = xt_sb_jd_DAL.Find(hash2);

            Hashtable hash3 = new Hashtable();
            hash3.Add("company_id",company_id);

            IEnumerator<TableXtCompanySbInfoEntity> sb_ator = xt_company_sb_DAL.Find(hash3, SqlOperator.And, true).GetEnumerator();

            while (sb_ator.MoveNext())
            {
                Table_xt_waterproperty_entity wt_entity = xt_waterproperty_DAL.FindByID(sb_ator.Current.Wp_id);

                if (wt_entity.Money > max)
                    max = wt_entity.Money;
            }

            if (sb_jd_entity.Jd_scjh == 0) return null;
     
            if (sb_jd_entity.Jd_scjh < sb_jd_entity.Jd_cjh)
                info.Num = sb_jd_entity.Jd_scjh;
            else
                info.Num = sb_jd_entity.Jd_cjh;

            info.Money = max;
            info.All_money = Convert.ToDecimal(info.Num * info.Money);

            return info;
        }
        
        public BindingList<BEInvoiceSbJDInfo> Query(int year, int jd, int company_id)
        {
            BindingList<BEInvoiceSbJDInfo> list = new BindingList<BEInvoiceSbJDInfo>();

            Hashtable hash = new Hashtable();

            hash.Add("sb_year", year);
            hash.Add("sb_jd", jd);
            hash.Add("company_id", company_id);

            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                BEInvoiceSbJDInfo info = new BEInvoiceSbJDInfo();
                info.Jd_cjh = ator.Current.Jd_cjh;
                info.Jd_jh = ator.Current.Jd_jh;
                info.Id = ator.Current.Id;
                info.Jd_jh_begin = ator.Current.Jd_jh_begin;
                info.Jd_sum = ator.Current.Jd_sum;
                info.Jd_scjh = ator.Current.Jd_scjh;
                info.Sb_year = ator.Current.Sb_year;
                info.Sb_jd = ator.Current.Sb_jd;

                list.Add(info);
            }

            return list;
        }
    }
}
